/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vehicles;

/**
 * A JavaBean that represents a Car.
 *
 * @author jonesbr
 */
public abstract class Car {
    public static final String FIELD_DELIMITER = "\t";
    public static final String LINE_DELIMITER = "\n";

    protected int odometer;
    protected int tripOdometer;
    protected double gallonsOfGas;
    protected int milesPerGallon;
    protected double gasConsumed;
    protected static double pricePerGallon;
    protected String carType;

    /**
     * Define the type of car we are.
     * @return 
     */
    public abstract String getCarClass();
    
    /**
     * Save the state of this object as a String.
     * @return 
     */
    public String convertToString() {
        String carClass = getCarClass();
        return carClass + FIELD_DELIMITER + odometer + FIELD_DELIMITER + tripOdometer + FIELD_DELIMITER + gallonsOfGas + FIELD_DELIMITER + milesPerGallon + LINE_DELIMITER;
    }
    
    /**
     * Receive a String and convert it to an object by parsing the String.
     * Assume we are only parsing one line.
     * @param data 
     */
    public static Car convertFromString(String data) {
        // find our delimiter positions.
        int firstDelimiter = data.indexOf(FIELD_DELIMITER);
        int secondDelimiter = data.indexOf(FIELD_DELIMITER, firstDelimiter + 1);
        int thirdDelimiter = data.indexOf(FIELD_DELIMITER, secondDelimiter + 1);
        int fourthDelimiter = data.indexOf(FIELD_DELIMITER, thirdDelimiter + 1);
        
        // first, find out what car type we have.
        String carType = data.substring(0,firstDelimiter);
        
        // next, create an object that matches that type.
        Car thisCar = Driver.createCar(carType);
        
        // parse through the information.
        String strOdometer = data.substring(firstDelimiter+1, secondDelimiter);
        int intOdometer = Integer.parseInt(strOdometer);
        thisCar.setOdometer(intOdometer);
        
        String strTripOdometer = data.substring(secondDelimiter + 1, thirdDelimiter);
        int intTripOdometer = Integer.parseInt(strTripOdometer);
        thisCar.setTripOdometer(intTripOdometer);
        
        String strGallonsOfGas = data.substring(thirdDelimiter + 1 , fourthDelimiter);
        double dblGallonsOfgas = Double.parseDouble(strGallonsOfGas);
        thisCar.setGallonsOfGas(dblGallonsOfgas);
        
        String strMilesPerGallon = data.substring(fourthDelimiter +1);
        int intMilesPerGallon = Integer.parseInt(strMilesPerGallon);
        thisCar.setMilesPerGallon(intMilesPerGallon);
        
        // return the populated car.
        return thisCar;
        
        
        
    }
    
    public String toString() {
        // declare the varaible that holds the data we will return.
        String state = "Operating Cost : " + pricePerGallon * gasConsumed + " Odometer " + odometer + " Gallons of Gas: " + gallonsOfGas + " trip odometer: " + tripOdometer;


        // return the state.
        return state;

    }

    /**
     * Move the car a specified distance
     *
     * @param distance the distance we wish to move the car.
     */
    public void go(int distance) {

        // compute the gallons of gas used.
        double gallonsOfGasUsed = distance / milesPerGallon;

        // add gallonsOfGasUsed to the running tally; gasConsumed.
        gasConsumed += gallonsOfGasUsed;

        // subtract the gallons used from the gallons available.
        gallonsOfGas = gallonsOfGas - gallonsOfGasUsed;

        // increase the odometer.
        odometer = odometer + distance;

        // increase the trip odometer.
        tripOdometer += distance;
    }

    /**
     * @return the odometer
     */
    public int getOdometer() {
        return odometer;
    }

    /**
     * @param odometer the odometer to set
     */
    public void setOdometer(int odometer) {
        this.odometer = odometer;
    }

    /**
     * @return the tripOdometer
     */
    public int getTripOdometer() {
        return tripOdometer;
    }

    /**
     * @param tripOdometer the tripOdometer to set
     */
    public void setTripOdometer(int tripOdometer) {
        this.tripOdometer = tripOdometer;
    }

    /**
     * @return the gallonsOfGas
     */
    public double getGallonsOfGas() {
        return gallonsOfGas;
    }

    /**
     * @param gallonsOfGas the gallonsOfGas to set
     */
    public void setGallonsOfGas(double gallonsOfGas) {
        this.gallonsOfGas = gallonsOfGas;
    }

    /**
     * @return the milesPerGallon
     */
    public int getMilesPerGallon() {
        return milesPerGallon;
    }

    /**
     * @param milesPerGallon the milesPerGallon to set
     */
    public void setMilesPerGallon(int milesPerGallon) {
        this.milesPerGallon = milesPerGallon;
    }

    /**
     * @return the gasConsumed
     */
    public double getGasConsumed() {
        return gasConsumed;
    }

    /**
     * @param gasConsumed the gasConsumed to set
     */
    public void setGasConsumed(double gasConsumed) {
        this.gasConsumed = gasConsumed;
    }

    /**
     * @return the pricePerGallon
     */
    public static double getPricePerGallon() {
        return pricePerGallon;
    }

    /**
     * @param aPricePerGallon the pricePerGallon to set
     */
    public static void setPricePerGallon(double aPricePerGallon) {
        pricePerGallon = aPricePerGallon;
    }

    /**
     * @return the carType
     */
    public String getCarType() {
        return carType;
    }

    /**
     * @param carType the carType to set
     */
    public void setCarType(String carType) {
        this.carType = carType;
    }
}
